//
//	NeutriumJS Steam
//	https://github.com/NativeDynamics/NeutriumJS.Steam
//
//	Copyright 2015, Native Dynamics
//	https://neutrium.net
//
//	Licensed under the Creative Commons Attribution 4.0 International
//	http://creativecommons.org/licenses/by/4.0/legalcode
//

(function (root, factory) {
    "use strict";

	if(typeof define === "function" && define.amd)
	{
		define('NeutriumJS/thermo/IAWPS97/PT', ['NeutriumJS/thermo/IAPWS97'], factory);
	}
	else if (typeof exports === "object" && module.exports)
	{
		module.exports = factory(require('NeutriumJS.thermo.IAPWS97'));
	}
	else
	{
		root.NeutriumJS = root.NeutriumJS || {};
		root.NeutriumJS.thermo = root.NeutriumJS.thermo || {};
		root.NeutriumJS.thermo.IAPWS97 = root.NeutriumJS.thermo.IAPWS97 || {};
		root.NeutriumJS.thermo.IAPWS97.PT = factory(root.NeutriumJS.thermo.IAPWS97);
	}
}(this, function(NS) {
	"use strict";

	// Private members
	var R = NS.CONST('R'),
		PT = {
			solve : solve,
			r1 : r1_PT,
			r2 : r2_PT,
			r3 : r3_PT,
			r5 : r5_PT,

			// Exposed for testing
			b23_P_T : b23_P_T,
			r4_T_Psat : r4_T_Psat,
			r4_P_Tsat : r4_P_Tsat,
			b23_T_P : b23_T_P,
			b3ab_P_T : b3ab_P_T,
			b3cd_P_T : b3cd_P_T,
			b3ef_P_T : b3ef_P_T,
			b3gh_P_T : b3gh_P_T,
			b3ij_P_T : b3ij_P_T,
			b3jk_P_T : b3jk_P_T,
			b3mn_P_T : b3mn_P_T,
			b3op_P_T : b3op_P_T,
			b3qu_P_T : b3qu_P_T,
			b3rx_P_T : b3rx_P_T,
			b3uv_P_T : b3uv_P_T,
			b3wx_P_T : b3wx_P_T,
		};

	return PT;

	//
	//	Comments : Calculate the steam properties using IAWPS for a given pressure and temperature
	//
	//	@param P is the pressure of the water in MPa
	//	@param T is the temperature in K
	//
	function solve(P, T, v)
	{

		v = (typeof v == "undefined") ? -1 : v;

		var region = findRegion_PT(P,T),
			result = null;

		switch(region)
		{
			case 1  : result = r1_PT(P, T); break;
			case 2  : result = r2_PT(P, T); break;
			case 3  : result = r3_PT(P, T, v); break;
			case 5  : result = r5_PT(P, T); break;
			default : throw new NS.Exception();
		}

		return result;
	}

	//
	//	Comments : Determines which IAPWS-IF97 region a pressure and temperature combination lie in.
	//
	//	@param P is the pressure of the water in MPa
	//	@param T is the temperature in K
	//
	function findRegion_PT(P, T)
	{
		if (P >= NS.CONST('MIN_P') && P <= NS.CONST('MAX_P') && T >= NS.CONST('MIN_T') &&
				(T <= NS.CONST('R5_MIN_T') || ( P <= NS.CONST('R5_MAX_P') && T <= NS.CONST('MAX_T')))) {

			if (T > NS.CONST('R5_MIN_T')) {
				if(P <= NS.CONST('R5_MAX_P')) {
					return 5;
				} else {
					return -1;
				}
			} else if (T > NS.CONST('R3_MIN_T')) {
				if (P > b23_T_P(T)) {
					return 3;
				} else {
					return 2;
				}
			} else {
				if (r4_T_Psat(T) > P) {
					return 2;
				} else {
					return 1;
				}
			}
		}

		return -1;
	}

	function generateResult(r)
	{
		return new NS.SteamResult(r);
	}

	// Auxillary equations for Regions 2 and 3

	// Equation 5 (pp. 5) of IAPWS-97
	// Returns the pressure for a given temperature along the region 2-3 boundary
	function b23_T_P(T)
	{
		return 348.05185628969 - 1.1671859879975*T+ 0.0010192970039326*T*T;
	}

	// Equation 6 (pp. 6) of IAPWS-97
	// Returns the temperature for a given pressure along the region 2-3 boundary
	function b23_P_T(P)
	{
		return 572.54459862746 + Math.pow((P-13.91883977887)/0.0010192970039326, 0.5);
	}

	// Region 1 forwards equation
	function r1_PT(P, T)
	{
		var R1_PT_N = [0.14632971213167, -0.84548187169114, -3.756360367204, 3.3855169168385, -0.95791963387872, 0.15772038513228, -0.016616417199501, 8.1214629983568E-4, 2.8319080123804E-4, -6.0706301565874E-4, -0.018990068218419, -0.032529748770505, -0.021841717175414, -5.283835796993E-5, -4.7184321073267E-4, -3.0001780793026E-4, 4.7661393906987E-5, -4.4141845330846E-6, -7.2694996297594E-16, -3.1679644845054E-5, -2.8270797985312E-6, -8.5205128120103E-10, -2.2425281908E-6, -6.5171222895601E-7, -1.4341729937924E-13, -4.0516996860117E-7, -1.2734301741641E-9, -1.7424871230634E-10, -6.8762131295531E-19, 1.4478307828521E-20, 2.6335781662795E-23, -1.1947622640071E-23, 1.8228094581404E-24, -9.3537087292458E-26],
			R1_PT_J = [-2, -1, 0, 1, 2, 3, 4, 5, -9, -7, -1, 0, 1, 3, -3, 0, 1, 3, 17, -4, 0, 6, -5, -2, 10, -8, -11, -6, -29, -31, -38, -39, -40, -41],
			R1_PT_I = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 8, 8, 21, 23, 29, 30, 31, 32],
			tau = 1386.0/T,
			pi = P/16.53,
			g = 0,
			g_p = 0,
			g_pp = 0,
			g_t = 0,
			g_tt = 0,
			g_pt = 0;

		for (var i = 0; i < 34; i++) {
			var I = R1_PT_I[i],
				J = R1_PT_J[i],
				N = R1_PT_N[i];

			g += N*Math.pow(7.1-pi, I)*Math.pow(tau-1.222, J);

			g_p += -N*I*Math.pow(7.1-pi, I-1)*Math.pow(tau-1.222, J);
			g_pp += N*I*(I-1)*Math.pow(7.1-pi, I-2)*Math.pow(tau-1.222, J);

			g_t += N*Math.pow(7.1-pi, I)*J*Math.pow(tau-1.222, J-1);
			g_tt += N*Math.pow(7.1-pi, I)*J*(J-1)*Math.pow(tau-1.222,J-2);

			g_pt += -N*I*Math.pow(7.1-pi, I-1)*J*Math.pow(tau-1.222, J-1);
		}

		return generateResult({
			p: P,
			t: T,
			v: R*T*pi*g_p/(1000*P),
			u: R*T*(tau*g_t-pi*g_p),
			s: R*(tau*g_t-g),
			h: R*T*tau*g_t,
			cp: R*(-Math.pow(tau,2)*g_tt),
			cv: R*(-Math.pow(tau,2)*g_tt+Math.pow(g_p-tau*g_pt,2)/g_pp),
			w: Math.pow(1000*R*T*Math.pow(g_p,2) / (Math.pow(g_p-tau*g_pt,2) / (Math.pow(tau,2)*g_tt) - g_pp), 0.5)
		});
	}

	function r2_PT(P, T)
	{
		var R2I_PT_N = [-9.6927686500217, 10.086655968018, -0.005608791128302, 0.071452738081455, -0.40710498223928, 1.4240819171444, -4.383951131945, -0.28408632460772, 0.021268463753307],
			R2I_PT_J = [0, 1, -5, -4, -3, -2, -1, 2, 3],
			R2R_PT_N = [-1.7731742473213E-3, -0.017834862292358, -0.045996013696365, -0.057581259083432, -0.05032527872793, -3.3032641670203E-5, -1.8948987516315E-4, -3.9392777243355E-3, -0.043797295650573, -2.6674547914087E-5, 2.0481737692309E-8, 4.3870667284435E-7, -3.227767723857E-5, -1.5033924542148E-3, -0.040668253562649, -7.8847309559367E-10, 1.2790717852285E-8, 4.8225372718507E-7, 2.2922076337661E-6, -1.6714766451061E-11, -2.1171472321355E-3, -23.895741934104, -5.905956432427E-18, -1.2621808899101E-6, -0.038946842435739, 1.1256211360459E-11, -8.2311340897998, 1.9809712802088E-8, 1.0406965210174E-19, -1.0234747095929E-13, -1.0018179379511E-9, -8.0882908646985E-11, 0.10693031879409, -0.33662250574171, 8.9185845355421E-25, 3.0629316876232E-13, -4.2002467698208E-6, -5.9056029685639E-26, 3.7826947613457E-6, -1.2768608934681E-15, 7.3087610595061E-29, 5.5414715350778E-17, -9.436970724121E-7],
			R2R_PT_J = [0, 1, 2, 3, 6, 1, 2, 4, 7, 36, 0, 1, 3, 6, 35, 1, 2, 3, 7, 3, 16, 35, 0, 11, 25, 8, 36, 13, 4, 10, 14, 29, 50, 57, 20, 35, 48, 21, 53, 39, 26, 40, 58],
			R2R_PT_I = [1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 5, 6, 6, 6, 7, 7, 7, 8, 8, 9, 10, 10, 10, 16, 16, 18, 20, 20, 20, 21, 22, 23, 24, 24, 24],
			pi = P,
			tau = 540/T,
			gi = Math.log(pi),
			gi_p = 1/pi,
			gi_pp = -1/(pi*pi),
			gi_t = 0,
			gi_tt = 0,
			gi_pt = 0,
			N, J, I;

		for (var i = 0; i < 9; i++)
		{
			N = R2I_PT_N[i];
			J = R2I_PT_J[i];

			gi += N*Math.pow(tau, J);
			gi_t += N*J*Math.pow(tau, J-1);
			gi_tt += N*J*(J-1)*Math.pow(tau, J-2);
		}

		var gr = 0,
			gr_p = 0,
			gr_pp = 0,
			gr_t = 0,
			gr_tt = 0,
			gr_pt = 0;

		for (var j = 0; j < 43; j++) {
			N = R2R_PT_N[j];
			J = R2R_PT_J[j];
			I = R2R_PT_I[j];

			gr += N*Math.pow(pi, I)*Math.pow(tau-0.5, J);
			gr_p += N*I*Math.pow(pi, I-1)*Math.pow(tau-0.5, J);
			gr_pp += N*I*(I-1)*Math.pow(pi, I-2)*Math.pow(tau-0.5, J);
			gr_t += N*Math.pow(pi, I)*J*Math.pow(tau-0.5, J-1);
			gr_tt += N*Math.pow(pi, I)*J*(J-1)*Math.pow(tau-0.5, J-2);
			gr_pt += N*I*Math.pow(pi, I-1)*J*Math.pow(tau-0.5, J-1);

		}

		return generateResult({
			p: P,
			t: T,
			v: (R*T/(1000*P))*pi*(gi_p + gr_p),
			u: R*T*(tau*(gi_t+gr_t)-pi*(gi_p+gr_p)),
			s: R*(tau*(gi_t+gr_t)-(gi+gr)),
			h: R*T*tau*(gi_t+gr_t),
			cp: -R*tau*tau*(gi_tt+gr_tt),
			cv: R*(-tau*tau*(gi_tt+gr_tt)-Math.pow(1+pi*gr_p-tau*pi*gr_pt,2)/(1-pi*pi*gr_pp)),
			w: Math.pow(1000*R*T*(1 + 2*pi*gr_p+pi*pi*gr_p*gr_p)/((1-pi*pi*gr_pp)+Math.pow(1+pi*gr_p-tau*pi*gr_pt,2)/(tau*tau*(gi_tt+gr_tt))), 0.5)
		});
	}

	//
	// Region 3
	//

	// Use a optional argument to calculate density
	function r3_PT(P, T, rho)
	{
		var v = 1/rho;

		if (rho == -1 || typeof rho == 'undefined') // Has the density volume been set - if false then calculate it
		{
			v = r3_PT_v(P, T);
			rho = 1/v;
		}

		var R3_PT_N = [1.0658070028513, -15.732845290239, 20.944396974307, -7.6867707878716, 2.6185947787954, -2.808078114862, 1.2053369696517, -8.4566812812502E-03, -1.2654315477714, -1.1524407806681, 0.88521043984318, -0.64207765181607, 0.38493460186671, -0.85214708824206, 4.8972281541877, -3.0502617256965, 0.039420536879154, 0.12558408424308, -0.2799932969871, 1.389979956946, -2.018991502357, -8.2147637173963E-03, -0.47596035734923, 0.0439840744735, -0.44476435428739, 0.90572070719733, 0.70522450087967, 0.10770512626332, -0.32913623258954, -0.50871062041158, -0.022175400873096, 0.094260751665092, 0.16436278447961, -0.013503372241348, -0.014834345352472, 5.7922953628084E-04, 3.2308904703711E-03, 8.0964802996215E-05, -1.6557679795037E-04, -4.4923899061815E-05],
			R3_PT_J = [0, 0, 1, 2, 7, 10, 12, 23, 2, 6, 15, 17, 0, 2, 6, 7, 22, 26, 0, 2, 4, 16, 26, 0, 2, 4, 26, 1, 3, 26, 0, 2, 26, 2, 26, 2, 26, 0, 1, 26],
			R3_PT_I = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 8, 9, 9, 10, 10, 11],
			g = rho/322,
			tau = 647.096/T,
			s = R3_PT_N[0]*Math.log(g),
			s_g = R3_PT_N[0]/g,
			s_gg = -R3_PT_N[0]/Math.pow(g,2),
			s_t = 0,
			s_tt = 0,
			s_gt = 0;

		// Need to start from i = 1 because j[0] and i[0] are NA and not used
		for (var i = 1; i < 40; i++)
		{
			var N = R3_PT_N[i],
				J = R3_PT_J[i],
				I = R3_PT_I[i];

			s += N*Math.pow(g,I)*Math.pow(tau,J);
			s_g += N*I*Math.pow(g,I-1.0)*Math.pow(tau,J);
			s_gg += N*I*(I-1.0)*Math.pow(g,I-2.0)*Math.pow(tau,J);
			s_t += N*Math.pow(g,I)*J*Math.pow(tau,J-1.0);
			s_tt += N*Math.pow(g,I)*J*(J-1.0)*Math.pow(tau,J-2.0);
			s_gt += N*I*Math.pow(g,I-1.0)*J*Math.pow(tau,J-1.0);
		}

		return generateResult({
			p: rho*R*T*g*s_g/1000,
			t: T,
			v: v,
			u: R*T*tau*s_t,
			s: R*(tau*s_t-s),
			h: R*T*(tau*s_t+g*s_g),
			cp: R*(-Math.pow(tau,2)*s_tt + Math.pow(g*s_g-g*tau*s_gt,2)/(2*g*s_g+Math.pow(g,2)*s_gg)),
			cv: R*(-Math.pow(tau,2)*s_tt),
			w: Math.pow(1000*R*T*(2*g*s_g + Math.pow(g,2)*s_gg - Math.pow(g*s_g-g*tau*s_gt,2)/(Math.pow(tau,2)*s_tt)),0.5)
		});
	}

	// Selection criteria from table 2, page 10 of PT Region 3 supplementary release
	function r3_PT_v(P, T)
	{
		var Tsat = r4_P_Tsat(P),
			b3cd = b3cd_P_T(P);

		if( 40.0 < P && P <= 100.0 )
		{
			if(T <= b3ab_P_T(P))
			{
				return r3a_PT_v(P,T);
			}
			else
			{
				return r3b_PT_v(P,T);
			}
		}
		else if( 25.0 < P && P <= 40.0 )
		{
			if( T <= b3cd )
			{
				return r3c_PT_v(P,T);
			}
			else if( T <= b3ab_P_T(P) )
			{
				return r3d_PT_v(P,T);
			}
			else if( T <= b3ef_P_T(P) )
			{
				return r3e_PT_v(P,T);
			}
			else
			{
				return r3f_PT_v(P,T);
			}
		}
		else if ( 22.5 < P && P <= 25.0)
		{
			if( T <= b3cd )
			{
				return r3c_PT_v(P,T);
			}
			else if (T <= b3gh_P_T(P))
			{
				if( P <= 23.5 )
				{
					return r3l_PT_v(P,T);
				}

				return r3g_PT_v(P,T);
			}
			else if ( T <= b3ef_P_T(P) )
			{
				if( P <= 23.0 )
				{
					if(T <= b3mn_P_T(P))
					{
						return r3m_PT_v(P,T);
					}
					else
					{
						return r3n_PT_v(P,T);
					}
				}

				return r3h_PT_v(P,T);
			}
			else if ( T <= b3ij_P_T(P) )
			{
				if ( P <= 23.0 )
				{
					if( T <= b3op_P_T(P) )
					{
						return r3o_PT_v(P,T);
					}
					else
					{
						return r3p_PT_v(P,T);
					}
				}

				return r3i_PT_v(P,T);
			}
			else if( T <= b3jk_P_T(P) )
			{
				return r3j_PT_v(P,T);
			}
			else
			{
				return r3k_PT_v(P,T);
			}
		}
		else if ( 21.04336732 < P && P <= 22.5) // R4_T_Psat(643.15) < P < 22.5
		{
			if( T <= b3cd )
			{
				return r3c_PT_v(P,T);
			}
			else if( T <= b3qu_P_T(P) )
			{
				return r3q_PT_v(P,T);
			}
			else if( T <= b3rx_P_T(P) )
			{
				//critical region
				if( P <= 22.064 )
				{
					//Subcritical pressure region
					if( T <= Tsat )
					{
						if( P <= 21.93161551 )
						{
							return r3u_PT_v(P,T);
						}
						else
						{
							if( T <= b3uv_P_T(P) )
							{
								return r3u_PT_v(P,T);
							}
							else
							{
								return r3y_PT_v(P,T);
							}
						}
					}
					else
					{
						if( P <= 21.90096265 )
						{
							return r3x_PT_v(P,T);
						}
						else
						{
							if( T <= b3wx_P_T(P) )
							{
								return r3z_PT_v(P,T);
							}
							else
							{
								return r3x_PT_v(P,T);
							}
						}
					}
				}
				else
				{
					//Supercritical pressure region
					if( T <= b3uv_P_T(P) )
					{
						return r3u_PT_v(P,T);
					}
					else if( T <= b3ef_P_T(P) )
					{
						if( P <= 22.11)
						{
							return r3y_PT_v(P,T);
						}
						else
						{
							return r3v_PT_v(P,T);
						}
					}
					else if( T <= b3wx_P_T(P) )
					{
						if( P <= 22.11 )
						{
							return r3z_PT_v(P,T);
						}
						else
						{
							return r3w_PT_v(P,T);
						}
					}
					else
					{
						return r3x_PT_v(P,T);
					}
				}
			}
			else if( T <= b3jk_P_T(P) )
			{
				return r3r_PT_v(P,T);
			}
			else
			{
				return r3k_PT_v(P,T);
			}
		}
		else if ( 20.5 < P && P <= 21.04336732)
		{
			if( T <= b3cd )
			{
				return r3c_PT_v(P,T);
			}
			else if( T <= Tsat )
			{
				return r3s_PT_v(P,T);
			}
			else if( T <= b3jk_P_T(P) )
			{
				return r3r_PT_v(P,T);
			}
			else
			{
				return r3k_PT_v(P,T);
			}
		}
		else if ( 19.00881189173929 < P && P <= 20.5 )
		{
			if( T <= b3cd )
			{
				return r3c_PT_v(P,T);
			}
			else if( T <= Tsat ) {
				return r3s_PT_v(P,T);
			}
			else
			{
				return r3t_PT_v(P,T);
			}
		}
		else if ( 16.52916427498 < P && P <= 19.00881189173929 ) // R4_T_Psat(623.15) < P < P3cd
		{
			if( T <= Tsat )
			{
				return r3c_PT_v(P,T);
			}
			else
			{
				return r3t_PT_v(P,T);
			}
		}

		// Else specific volume cannot be calculated and set to -ve
		return -1.0;
	}

	// Subregion Boundary equations for Region 3
	// From pp. 9 of Supplementary Release on Backward Equations for Specific Volume as a Function of Pressure and Temperature v(p,T)
	function b3ab_P_T(P)
	{
		var B3ab_PT_I = [0,1,2,-1,-2],
			B3ab_PT_N = [1.54793642129415E3,-1.87661219490113E2,2.13144632222113E1,-1.91887498864292E3,9.18419702359447E2],
			T = 0;

		P = Math.log(P);

		for (var i = 0; i < 5; i++)
		{
			T += B3ab_PT_N[i]*Math.pow(P, B3ab_PT_I[i]);
		}

		return T;
	}

	function b3op_P_T(P)
	{
		var B3op_PT_I = [0,1,2,-1,-2],
			B3op_PT_N = [9.69461372400213E2,-3.32500170441278E2,6.4285959846667E1,7.73845935768222E2,-1.52313732937084E3],
			T = 0;

		P = Math.log(P);

		for (var i = 0; i < 5; i++)
		{
			T += B3op_PT_N[i]*Math.pow(P, B3op_PT_I[i]);
		}

		return T;
	}

	function b3ef_P_T(P)
	{
		return 3.727888004*(P-22.064)+647.096;
	}

	function b3cd_P_T(P)
	{
		var B3cd_PT_I = [0,1,2,3],
			B3cd_PT_N = [5.85276966696349E2,2.78233532206915,-1.27283549295878E-2,1.59090746562729E-4],
			T = 0;

		for (var i = 0; i < 4; i++)
		{
			T += B3cd_PT_N[i]*Math.pow(P, B3cd_PT_I[i]);
		}

		return T;
	}

	function b3gh_P_T(P)
	{
		var B3gh_PT_I = [0,1,2,3,4],
			B3gh_PT_N = [-2.49284240900418E4,4.28143584791546E3,-2.6902917314013E2,7.51608051114157,-7.87105249910383E-2],
			T = 0;

		for(var i = 0; i < 5; i++)
		{
			T += B3gh_PT_N[i]*Math.pow(P, B3gh_PT_I[i]);
		}

		return T;
	}

	function b3ij_P_T(P)
	{
		var B3ij_PT_I = [0,1,2,3,4],
			B3ij_PT_N = [5.84814781649163E2,-6.16179320924617E-1,2.60763050899562E-1,-5.87071076864459E-3,5.15308185433082E-5],
			T = 0;

		for (var i = 0; i < 5; i++)
		{
			T += B3ij_PT_N[i]*Math.pow(P, B3ij_PT_I[i]);
		}

		return T;
	}

	function b3jk_P_T(P)
	{
		var B3jk_PT_I = [0,1,2,3,4],
			B3jk_PT_N = [6.17229772068439E2,-7.70600270141675,6.97072596851896E-1,-1.57391839848015E-2,1.37897492684194E-4],
			T = 0;

		for(var i = 0; i < 5; i++)
		{
			T += B3jk_PT_N[i]*Math.pow(P, B3jk_PT_I[i]);
		}

		return T;
	}

	function b3mn_P_T(P)
	{
		var B3mn_PT_I = [0,1,2,3],
			B3mn_PT_N = [5.35339483742384E2,7.61978122720128,-1.58365725441648E-1,1.92871054508108E-3],
			T = 0;

		for(var i = 0; i < 4; i++)
		{
			T += B3mn_PT_N[i]*Math.pow(P, B3mn_PT_I[i]);
		}

		return T;
	}

	function b3qu_P_T(P)
	{
		var B3qu_PT_I = [0,1,2,3],
			B3qu_PT_N = [5.65603648239126E2,5.29062258221222,-1.02020639611016E-1,1.22240301070145E-3],
			T = 0;

		for(var i = 0; i < 4; i++)
		{
			T += B3qu_PT_N[i]*Math.pow(P, B3qu_PT_I[i]);
		}

		return T;
	}

	function b3rx_P_T(P)
	{
		var B3rx_PT_I = [0,1,2,3],
			B3rx_PT_N = [5.84561202520006E2,-1.02961025163669,2.43293362700452E-1,-2.94905044740799E-3],
			T = 0;

		for(var i = 0; i < 4; i++)
		{
			T += B3rx_PT_N[i]*Math.pow(P, B3rx_PT_I[i]);
		}

		return T;
	}

	function r3a_PT_v(P, T)
	{
		var pi = P/100,
			sig = T/760,
			v = 0,
			R3a_PT_I = [-12,-12,-12,-10,-10,-10,-8,-8,-8,-6,-5,-5,-5,-4,-3,-3,-3,-3,-2,-2,-2,-1,-1,-1,0,0,1,1,2,2],
			R3a_PT_J = [5,10,12,5,10,12,5,8,10,1,1,5,10,8,0,1,3,6,0,2,3,0,1,2,0,1,0,2,0,2],
			R3a_PT_N = [1.10879558823853E-3,5.72616740810616E2,-7.67051948380852E4,-2.53321069529674E-2,6.28008049345689E3,2.34105654131876E5,2.16867826045856E-1,-1.56237904341963E2,-2.69893956176612E4,-1.80407100085505E-4,1.16732227668261E-3,2.6698704085604E1,2.82776617243286E4,-2.42431520029523E3,4.35217323022733E-4,-1.22494831387441E-2,1.79357604019989,4.42729521058314E1,-5.93223489018342E-3,4.53186261685774E-1,1.3582570312914,4.08748415856745E-2,4.74686397863312E-1,1.18646814997915,5.46987265727549E-1,1.95266770452643E-1,-5.02268790869663E-2,-3.69645308193377E-1,6.3382803752842E-3,7.97441793901017E-2];

		for(var i = 0; i < 30; i++)
		{
			var N = R3a_PT_N[i],
				I = R3a_PT_I[i],
				J = R3a_PT_J[i];

			v += N*Math.pow(pi-0.085,I)*Math.pow(sig-0.817, J);
		}

		return 0.0024*v;
	}

	function r3b_PT_v(P, T)
	{
		var pi = P/100,
			sig = T/860,
			v = 0,
			R3b_PT_I = [-12,-12,-10,-10,-8,-6,-6,-6,-5,-5,-5,-4,-4,-4,-3,-3,-3,-3,-3,-2,-2,-2,-1,-1,0,0,1,1,2,3,4,4],
			R3b_PT_J = [10,12,8,14,8,5,6,8,5,8,10,2,4,5,0,1,2,3,5,0,2,5,0,2,0,1,0,2,0,2,0,1],
			R3b_PT_N = [-8.27670470003621E-2,4.16887126010565E1,4.83651982197059E-2,-2.91032084950276E4,-1.11422582236948E2,-2.02300083904014E-2,2.94002509338515E2,1.40244997609658E2,-3.44384158811459E2,3.61182452612149E2,-1.40699677420738E3,-2.02023902676481E-3,1.71346792457471E2,-4.25597804058632,6.91346085000334E-6,1.51140509678925E-3,-4.16375290166236E-2,-4.13754957011042E1,-5.06673295721637E1,-5.72212965569023E-4,6.08817368401785,2.39600660256161E1,1.22261479925384E-2,2.16356057692938,3.98198903368642E-1,-1.16892827834085E-1,-1.02845919373532E-1,-4.92676637589284E-1,6.5554045640679E-2,-2.4046253507853E-1,-2.69798180310075E-2,1.28369435967012E-1];

		for (var i = 0; i < 32; i++)
		{
			var N = R3b_PT_N[i],
				I = R3b_PT_I[i],
				J = R3b_PT_J[i];

			v += N*Math.pow(pi-0.280,I)*Math.pow(sig-0.779,J);
		}

		return 0.0041*v;
	}

	function r3c_PT_v(P, T)
	{
		var pi = P/40,
			sig = T/690,
			v = 0,
			R3c_PT_I = [-12,-12,-12,-10,-10,-10,-8,-8,-8,-6,-5,-5,-5,-4,-4,-3,-3,-2,-2,-2,-1,-1,-1,0,0,0,1,1,2,2,2,2,3,3,8],
			R3c_PT_J = [6,8,10,6,8,10,5,6,7,8,1,4,7,2,8,0,3,0,4,5,0,1,2,0,1,2,0,2,0,1,3,7,0,7,1],
			R3c_PT_N = [3.1196778876303,2.76713458847564E4,3.22583103403269E7,-3.42416065095363E2,-8.99732529907377E5,-7.93892049821251E7,9.53193003217388E1,2.29784742345072E3,1.75336675322499E5,7.91214365222792E6,3.19933345844209E-5,-6.59508863555767E1,-8.33426563212851E5,6.45734680583292E-2,-3.82031020570813E6,4.06398848470079E-5,3.10327498492008E1,-8.92996718483724E-4,2.34604891591616E2,3.77515668966951E3,1.58646812591361E-2,7.07906336241843E-1,1.2601622514657E1,7.36143655772152E-1,6.76544268999101E-1,-1.78100588189137E1,-1.56531975531713E-1,1.17707430048158E1,8.40143653860447E-2,-1.86442467471949E-1,-4.40170203949645E1,1.23290423502494E6,-2.40650039730845E-2,-1.07077716660869E6,4.38319858566475E-2];

		for(var i = 0; i < 35; i++)
		{
			var N = R3c_PT_N[i],
				I = R3c_PT_I[i],
				J = R3c_PT_J[i];

			v += N*Math.pow(pi-0.259,I)*Math.pow(sig-0.903, J);
		}
		return 0.0022*v;
	}

	function r3d_PT_v(P, T)
	{
		var pi = P/40,
			sig = T/690,
			v = 0,
			R3d_PT_I = [-12,-12,-12,-12,-12,-12,-10,-10,-10,-10,-10,-10,-10,-8,-8,-8,-8,-6,-6,-5,-5,-5,-5,-4,-4,-4,-3,-3,-2,-2,-1,-1,-1,0,0,1,1,3],
			R3d_PT_J = [4,6,7,10,12,16,0,2,4,6,8,10,14,3,7,8,10,6,8,1,2,5,7,0,1,7,2,4,0,1,0,1,5,0,2,0,6,0],
			R3d_PT_N = [-4.52484847171645E-10,3.15210389538801E-5,-2.14991352047545E-3,5.08058874808345E2,-1.27123036845932E7,1.15371133120497E12,-1.97805728776273E-16,2.41554806033972E-11,-1.56481703640525E-6,2.772113468366250E-3,-2.03578994462286E1,1.44369489909053E6,-4.11254217946539E10,6.23449786243773E-6,-2.21774281146038E1,-6.89315087933158E4,-1.95419525060713E7,3.16373510564015E3,2.24040754426988E6,-4.36701347922356E-6,-4.04213852833996E-4,-3.48153203414663E2,-3.85294213555289E5,1.35203700099403E-7,1.34648383271089E-4,1.25031835351736E5,9.68123678455841E-2,2.25660517512438E2,-1.90102435341872E-4,-2.99628410819229E-2,5.00833915372121E-3,3.87842482998411E-1,-1.38535367777182E3,8.70745245971773E-1,1.71946252068742,-3.26650121426383E-2,4.98044171727877E3,5.51478022765087E-3];

		for(var i = 0; i < 38; i++)
		{
			var N = R3d_PT_N[i],
				I = R3d_PT_I[i],
				J = R3d_PT_J[i];

			v += N*Math.pow(pi-0.559, I)*Math.pow(sig-0.939, J);
		}

		return 0.0029*Math.pow(v,4);
	}

	function r3e_PT_v(P, T)
	{
		var pi = P/40,
			sig = T/710,
			v = 0,
			R3e_PT_I = [-12,-12,-10,-10,-10,-10,-10,-8,-8,-8,-6,-5,-4,-4,-3,-3,-3,-2,-2,-2,-2,-1,0,0,1,1,1,2,2],
			R3e_PT_J = [14,16,3,6,10,14,16,7,8,10,6,6,2,4,2,6,7,0,1,3,4,0,0,1,0,4,6,0,2],
			R3e_PT_N = [7.15815808404721E8,-1.14328360753449E11,3.7653100201572E-12,-9.03983668691157E-5,6.65695908836252E5,5.35364174960127E9,7.94977402335603E10,9.22230563421437E1,-1.42586073991215E5,-1.11796381424162E6,8.9612162964076E3,-6.69989239070491E3,4.51242538486834E-3,-3.39731325977713E1,-1.20523111552278,4.75992667717124E4,-2.66627750390341E5,-1.53314954386524E-4,3.05638404828265E-1,1.23654999499486E2,-1.04390794213011E3,-1.57496516174308E-2,6.85331118940253E-1,1.78373462873903,-5.4467412487891E-1,2.04529931318843E3,-2.28342359328752E4,4.13197481515899E-1,-3.41931835910405E1];

		for(var i = 0; i < 29; i++)
		{
			var N = R3e_PT_N[i],
				I = R3e_PT_I[i],
				J = R3e_PT_J[i];

			v += N*Math.pow(pi-0.587, I)*Math.pow(sig-0.918, J);
		}

		return 0.0032*v;
	}

	function r3f_PT_v(P, T)
	{
		var pi = P/40,
			sig = T/730,
			v = 0,
			R3f_PT_I = [0,0,0,0,0,0,1,1,1,1,2,2,3,3,3,4,5,5,6,7,7,10,12,12,12,14,14,14,14,14,16,16,18,18,20,20,20,22,24,24,28,32],
			R3f_PT_J = [-3,-2,-1,0,1,2,-1,1,2,3,0,1,-5,-2,0,-3,-8,1,-6,-4,1,-6,-10,-8,-4,-12,-10,-8,-6,-4,-10,-8,-12,-10,-12,-10,-6,-12,-12,-4,-12,-12],
			R3f_PT_N = [-2.51756547792325E-8,6.01307193668763E-6,-1.00615977450049E-3,9.99969140252192E-1,2.14107759236486,-1.65175571959086E1,-1.41987303638727E-3,2.69251915156554,3.49741815858722E1,-3.00208695771783E1,-1.31546288252539,-8.39091277286169,1.81545608337015E-10,-5.91099206478909E-4,1.52115067087106,2.52956470663225E-5,1.00726265203786E-15,-1.4977453386065,-7.93940970562969E-10,-1.50290891264717E-4,1.51205531275133,4.70942606221652E-6,1.95049710391712E-13,-9.11627886266077E-9,6.04374640201265E-4,-2.25132933900136E-16,6.10916973582981E-12,-3.03063908043404E-7,-1.37796070798409E-5,-9.19296736666106E-4,6.39288223132545E-10,7.53259479898699E-7,-4.00321478682929E-13,7.56140294351614E-9,-9.12082054034891E-12,-2.37612381140539E-08,2.69586010591874E-5,-7.32828135157839E-11,2.4199557830666E-10,-4.05735532730322E-4,1.89424143498011E-10,-4.86632965074563E-10];

		for (var i = 0; i < 42; i++)
		{
			var N = R3f_PT_N[i],
				I = R3f_PT_I[i],
				J = R3f_PT_J[i];

			v += N*Math.pow(pi-0.587, 0.5*I)*Math.pow(sig-0.891, J);
		}

		return 0.0064*Math.pow(v,4);
	}

	function r3g_PT_v(P, T)
	{
		var pi = P/25,
			sig = T/660,
			v = 0,
			R3g_PT_I = [-12,-12,-12,-12,-12,-12,-10,-10,-10,-8,-8,-8,-8,-6,-6,-5,-5,-4,-3,-2,-2,-2,-2,-1,-1,-1,0,0,0,1,1,1,3,5,6,8,10,10],
			R3g_PT_J = [7,12,14,18,22,24,14,20,24,7,8,10,12,8,22,7,20,22,7,3,5,14,24,2,8,18,0,1,2,0,1,3,24,22,12,3,0,6],
			R3g_PT_N = [4.12209020652996E-5,-1.14987238280587E6,9.4818088503208E9,-1.95788865718971E17,4.962507048713E24,-1.05549884548496E28,-7.58642165988278E11,-9.22172769596101E22,7.25379072059348E29,-6.17718249205859E1,1.07555033344858E4,-3.79545802336487E7,2.28646846221831E11,-4.99741093010619E6,-2.80214310054101E30,1.04915406769586E6,6.13754229168619E27,8.02056715528378E31,-2.98617819828065E7,-9.10782540134681E1,1.35033227281565E5,-7.12949383408211E18,-1.04578785289542E36,3.04331584444093E1,5.93250797959445E9,-3.64174062110798E27,9.21791403532461E-1,-3.37693609657471E-1,-7.24644143758508E1,-1.10480239272601E-1,5.36516031875059,-2.91441872156205E3,6.16338176535305E39,-1.2088917586118E38,8.18396024524612E22,9.40781944835829E8,-3.67279669545448E4,-8.37513931798655E15];

		for (var i = 0; i < 38; i++)
		{
			var N = R3g_PT_N[i],
				I = R3g_PT_I[i],
				J = R3g_PT_J[i];

			v += N*Math.pow(pi-0.872, I)*Math.pow(sig-0.971, J);
		}

		return 0.0027*Math.pow(v,4);
	}

	function r3h_PT_v(P, T)
	{
		var pi = P/25,
			sig = T/660,
			v = 0,
			R3h_PT_I = [-12,-12,-10,-10,-10,-10,-10,-10,-8,-8,-8,-8,-8,-6,-6,-6,-5,-5,-5,-4,-4,-3,-3,-2,-1,-1,0,1,1],
			R3h_PT_J = [8,12,4,6,8,10,14,16,0,1,6,7,8,4,6,8,2,3,4,2,4,1,2,0,0,2,0,0,2],
			R3h_PT_N = [5.61379678887577E-2,7.74135421587083E9,1.11482975877938E-9,-1.43987128208183E-3,1.9369655876492E3,-6.05971823585005E8,1.71951568124337E13,-1.85461154985145E16,3.8785116807801E-17,-3.95464327846105E-14,-1.70875935679023E2,-2.1201062070122E3,1.77683337348191E7,1.10177443629575E1,-2.34396091693313E5,-6.56174421999594E6,1.56362212977396E-5,-2.129462570214,1.35249306374858E1,1.77189164145813E-1,1.39499167345464E3,-7.03670932036388E-3,-1.52011044389648E-1,9.81916922991113E-5,1.47199658618076E-3,2.02618487025578E1,8.9934551894424E-1,-2.11346402240858E-1,2.49971752957491E1];

		for (var i = 0; i < 29; i++)
		{
			var N = R3h_PT_N[i],
				I = R3h_PT_I[i],
				J = R3h_PT_J[i];

			v += N*Math.pow(pi-0.898, I)*Math.pow(sig-0.983, J);
		}
		return 0.0032*Math.pow(v, 4);
	}

	function r3i_PT_v(P, T)
	{
		var pi = P/25,
		sig = T/660,
		v = 0,
		R3i_PT_I = [0,0,0,1,1,1,1,2,3,3,4,4,4,5,5,5,7,7,8,8,10,12,12,12,14,14,14,14,18,18,18,18,18,20,20,22,24,24,32,32,36,36],
		R3i_PT_J = [0,1,10,-4,-2,-1,0,0,-5,0,-3,-2,-1,-6,-1,12,-4,-3,-6,10,-8,-12,-6,-4,-10,-8,-4,5,-12,-10,-8,-6,2,-12,-10,-12,-12,-8,-10,-5,-10,-8],
		R3i_PT_N = [1.06905684359136,-1.48620857922333,2.59862256980408E14,-4.46352055678749E-12,-5.66620757170032E-7,-2.35302885736849E-3,-2.69226321968839E-1,9.22024992944392,3.57633505503772E-12,-1.73942565562222E1,7.00681785556229E-6,-2.67050351075768E-4,-2.31779669675624,-7.53533046979752E-13,4.81337131452891,-2.23286270422356E21,-1.18746004987383E-5,6.46412934136496E-3,-4.10588536330937E-10,4.22739537057241E19,3.13698180473812E-13,1.6439533434504E-24,-3.39823323754373E-6,-1.35268639905021E-2,-7.232525142116250E-15,1.84386437538366E-9,-4.63959533752385E-2,-9.9226310037675E13,6.88169154439335E-17,-2.22620998452197E-11,-5.40843018624083E-8,3.45570606200257E-3,4.22275800304086E10,-1.26974478770487E-15,9.27237985153679E-10,6.126708120164890E-14,-7.22693924063497E-12,-3.83669502636822E-4,3.74684572410204E-4,-9.31976897511086E4,-2.47690616026922E-2,6.58110546759474E1];

		for (var i = 0; i < 42; i++)
		{
			var N = R3i_PT_N[i],
				I = R3i_PT_I[i],
				J = R3i_PT_J[i];

			v += N*Math.pow(pi-0.910, 0.5*I)*Math.pow(sig-0.984, J);
		}

		return 0.0041*Math.pow(v,4);
	}

	function r3j_PT_v(P, T)
	{
		var pi = P/25,
			sig = T/670,
			v = 0,
			R3j_PT_I = [0,0,0,1,1,1,2,2,3,4,4,5,5,5,6,10,12,12,14,14,14,16,18,20,20,24,24,28,28],
			R3j_PT_J = [-1,0,1,-2,-1,1,-1,1,-2,-2,2,-3,-2,0,3,-6,-8,-3,-10,-8,-5,-10,-12,-12,-10,-12,-6,-12,-5],
			R3j_PT_N = [-1.1137131739554E-4,1.00342892423685,5.30615581928979,1.79058760078792E-6,-7.28541958464774E-4,-1.87576133371704E1,1.99060874071849E-3,2.4357475537729E1,-1.77040785499444E-4,-2.5968038522713E-3,-1.98704578406823E2,7.38627790224287E-5,-2.36264692844138E-3,-1.61023121314333,6.22322971786473E3,-9.60754116701669E-9,-5.10572269720488E-11,7.67373781404211E-3,6.63855469485254E-15,-7.17590735526745E-10,1.46564542926508E-5,3.09029474277013E-12,-4.64216300971708E-16,-3.90499637961161E-14,-2.36716126781431E-10,4.54652854268717E-12,-4.22271787482497E-3,2.83911742354706E-11,2.70929002720228];

		for (var i = 0; i < 29; i++)
		{
			var N = R3j_PT_N[i],
				I = R3j_PT_I[i],
				J = R3j_PT_J[i];

			v += N*Math.pow(pi-0.875, 0.5*I)*Math.pow(sig-0.964,J);
		}

		return 0.0054*Math.pow(v,4);
	}

	function r3k_PT_v(P, T)
	{
		var pi = P/25,
			sig = T/680,
			v = 0,
			R3k_PT_I = [-2,-2,-1,-1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,2,2,2,2,2,2,5,5,5,6,6,6,6,8,10,12],
			R3k_PT_J = [10,12,-5,6,-12,-6,-2,-1,0,1,2,3,14,-3,-2,0,1,2,-8,-6,-3,-2,0,4,-12,-6,-3,-12,-10,-8,-5,-12,-12,-10],
			R3k_PT_N = [-4.01215699576099E8,4.84501478318406E10,3.94721471363678E-15,3.72629967374147E4,-3.69794374168666E-30,-3.80436407012452E-15,4.75361629970233E-7,-8.79148916140706E-4,8.44317863844331E-1,1.224331626566E1,-1.04529634830279E2,5.89702771277429E2,-2.91026851164444E13,1.7034307284185E-6,-2.77617606975748E-4,-3.44709605486686,2.21333862447095E1,-1.94646110037079E2,8.08354639772825E-16,-1.8084520914547E-11,-6.96664158132412E-6,-1.81057560300994E-3,2.55830298579027,3.28913873658481E3,-1.73270241249904E-19,-6.61876792558034E-7,-3.9568892342125E-3,6.04203299819132E-18,-4.00879935920517E-14,1.60751107464958E-9,3.83719409025556E-5,-6.49565446702457E-15,-1.49095328506E-12,5.41449377329581E-9];

		for (var i = 0; i < 34; i++)
		{
			var N = R3k_PT_N[i],
				I = R3k_PT_I[i],
				J = R3k_PT_J[i];

			v += N*Math.pow(pi-0.802,I)*Math.pow(sig-0.935,J);
		}

		return 0.0077*v;
	}

	function r3l_PT_v(P, T)
	{
		var pi = P/24,
			sig = T/650,
			v = 0,
			R3l_PT_I = [-12,-12,-12,-12,-12,-10,-10,-8,-8,-8,-8,-8,-8,-8,-6,-5,-5,-4,-4,-3,-3,-3,-3,-2,-2,-2,-1,-1,-1,0,0,0,0,1,1,2,4,5,5,6,10,10,14],
			R3l_PT_J = [14,16,18,20,22,14,24,6,10,12,14,18,24,36,8,4,5,7,16,1,3,18,20,2,3,10,0,1,3,0,1,2,12,0,16,1,0,0,1,14,4,12,10],
			R3l_PT_N = [2.60702058647537E9,-1.88277213604704E14,5.54923870289667E18,-7.58966946387758E22,4.13865186848908E26,-8.1503800073806E11,-3.81458260489955E32,-1.23239564600519E-2,2.26095631437174E7,-4.9501780950672E11,5.29482996422863E15,-4.44359478746295E22,5.21635864527315E34,-4.87095672740742E54,-7.14430209937547E5,1.27868634615495E-1,-1.00752127917598E1,7.7745143796099E6,-1.08105480796471E24,-3.57578581169659E-6,-2.12857169423484,2.70706111085238E29,-6.95953622348829E32,1.1060902747228E-1,7.21559163361354E1,-3.06367307532219E14,2.6583961888553E-5,2.53392392889754E-2,-2.14443041836579E2,9.37846601489667E-1,2.231840431017,3.38401222509191E1,4.94237237179718E20,-1.98068404154428E-1,-1.4141534988114E30,-9.93862421613651E1,1.25070534142731E2,-9.96473529004439E2,4.73137909872765E4,1.16662121219322E32,-3.15874976271533E15,-4.45703369196945E32,6.42794932373694E32];

		for(var i = 0; i < 43; i++)
		{
			var N = R3l_PT_N[i],
				I = R3l_PT_I[i],
				J = R3l_PT_J[i];

			v += N*Math.pow(pi-0.908, I)*Math.pow(sig-0.989, J);
		}

		return 0.0026*Math.pow(v, 4);
	}

	function r3m_PT_v(P, T)
	{
		var pi = P/23,
		sig = T/650,
		v = 0,
		R3m_PT_I = [0,3,8,20,1,3,4,5,1,6,2,4,14,2,5,3,0,1,1,1,28,2,16,0,5,0,3,4,12,16,1,8,14,0,2,3,4,8,14,24],
		R3m_PT_J = [0,0,0,2,5,5,5,5,6,6,7,8,8,10,10,12,14,14,18,20,20,22,22,24,24,28,28,28,28,28,32,32,32,36,36,36,36,36,36,36],
		R3m_PT_N = [8.11384363481847E-1,-5.68199310990094E3,-1.78657198172556E10,7.95537657613427E31,-8.14568209346872E4,-6.59774567602874E7,-1.52861148659302E10,-5.60165667510446E11,4.58384828593949E5,-3.85754000383848E13,4.53735800004273E7,9.39454935735563E11,2.66572856432938E27,-5.47578313899097E9,2.00725701112386E14,1.85007245563239E12,1.85135446828337E8,-1.70451090076385E11,1.57890366037614E14,-2.02530509748774E15,3.6819392618357E59,1.70215539458936E17,6.39234909918741E41,-8.21698160721956E14,-7.95260241872306E23,2.3341586947851E17,-6.00079934586803E22,5.94584382273384E24,1.89461279349492E39,-8.10093428842645E45,1.88813911076809E21,1.11052244098768E35,2.91133958602503E45,-3.2942192395146E21,-1.37570282536696E25,1.81508996303902E27,-3.46865122768353E29,-2.1196114877426E37,-1.28617899887675E48,4.79817895699239E64];

		for(var i = 0; i < 40; i++)
		{
			var N = R3m_PT_N[i],
				I = R3m_PT_I[i],
				J = R3m_PT_J[i];

			v += N*Math.pow(pi-1,I)*Math.pow(sig-0.997, 0.25*J);
		}

		return 0.0028*v;
	}

	function r3n_PT_v(P, T)
	{
		var pi = P/23,
		sig = T/650,
		v = 0,
		R3n_PT_I = [0,3,4,6,7,10,12,14,18,0,3,5,6,8,12,0,3,7,12,2,3,4,2,4,7,4,3,5,6,0,0,3,1,0,1,0,1,0,1],
		R3n_PT_J = [-12,-12,-12,-12,-12,-12,-12,-12,-12,-10,-10,-10,-10,-10,-10,-8,-8,-8,-8,-6,-6,-6,-5,-5,-5,-4,-3,-3,-3,-2,-1,-1,0,1,1,2,4,5,6],
		R3n_PT_N = [2.80967799943151E-39,6.14869006573609E-31,5.82238667048942E-28,3.90628369238462E-23,8.21445758255119E-21,4.02137961842776E-15,6.51718171878301E-13,-2.11773355803058E-8,2.64953354380072E-3,-1.35031446451331E-32,-6.07246643970893E-24,-4.02352115234494E-19,-7.44938506925544E-17,1.89917206526237E-13,3.64975183508473E-6,1.77274872361946E-26,-3.34952758812999E-19,-4.21537726098389E-9,-3.91048167929649E-2,5.41276911564176E-14,7.05412100773699E-12,2.58585887897486E-9,-4.93111362030162E-11,-1.58649699894543E-6,-5.250374278861E-1,2.20019901729615E-3,-6.43064132636925E-3,6.29154149015048E1,1.35147318617061E2,2.40560808321713E-7,-8.90763306701305E-4,-4.40209599407714E3,-3.02807107747776E2,1.59158748314599E3,2.32534272709876E5,-7.926812071326E5,-8.69871364662769E10,3.54542769185671E11,4.00849240129329E14];

		for(var i = 0; i < 39; i++)
		{
			var N = R3n_PT_N[i],
				I = R3n_PT_I[i],
				J = R3n_PT_J[i];

			v += N*Math.pow(pi-0.976, I)*Math.pow(sig-0.997, J);
		}

		return 0.0031*Math.exp(v);
	}

	function r3o_PT_v(P, T)
	{
		var pi = P/23,
			sig = T/650,
			v = 0,
			R3o_PT_I = [0,0,0,2,3,4,4,4,4,4,5,5,6,7,8,8,8,10,10,14,14,20,20,24],
			R3o_PT_J = [-12,-4,-1,-1,-10,-12,-8,-5,-4,-1,-4,-3,-8,-12,-10,-8,-4,-12,-8,-12,-8,-12,-10,-12],
			R3o_PT_N = [1.28746023979718E-35,-7.35234770382342E-12,2.8907869214915E-3,2.44482731907223E-1,1.41733492030985E-24,-3.54533853059476E-29,-5.94539202901431E-18,-5.85188401782779E-9,2.01377325411803E-6,1.38647388209306,-1.73959365084772E-5,1.37680878349369E-3,8.14897605805513E-15,4.25596631351839E-26,-3.87449113787755E-18,1.3981474793024E-13,-1.71849638951521E-3,6.41890529513296E-22,1.18960578072018E-11,-1.55282762571611E-18,2.33907907347507E-8,-1.74093247766213E-13,3.77682649089149E-9,-5.16720236575302E-11];

		for(var i = 0; i < 24; i++)
		{
			var N = R3o_PT_N[i],
				I = R3o_PT_I[i],
				J = R3o_PT_J[i];

			v += N*Math.pow(pi-0.974, 0.5*I)*Math.pow(sig-0.996, J);
		}

		return 0.0034*v;
	}

	function r3p_PT_v(P, T)
	{
		var pi = P/23,
			sig = T/650,
			v = 0,
			R3p_PT_I = [0,0,0,0,1,2,3,3,4,6,7,7,8,10,12,12,12,14,14,14,16,18,20,22,24,24,36],
			R3p_PT_J = [-1,0,1,2,1,-1,-3,0,-2,-2,-5,-4,-2,-3,-12,-6,-5,-10,-8,-3,-8,-8,-10,-10,-12,-8,-12],
			R3p_PT_N = [-9.82825342010366E-5,1.05145700850612,1.16033094095084E2,3.24664750281543E3,-1.23592348610137E3,-5.61403450013495E-2,8.56677401640869E-8,2.36313425393924E2,9.72503292350109E-3,-1.03001994531927,-1.49653706199162E-9,-2.15743778861592E-5,-8.34452198291445,5.86602660564988E-1,3.43480022104968E-26,8.16256095947021E-6,2.94985697916798E-3,7.11730466276584E-17,4.00954763806941E-10,1.07766027032853E1,-4.09449599138182E-7,-7.29121307758902E-6,6.77107970938909E-9,6.02745973022975E-8,-3.82323011855257E-11,1.79946628317437E-3,-3.45042834640005E-4];

		for(var i = 0; i < 27; i++)
		{
			var N = R3p_PT_N[i],
				I = R3p_PT_I[i],
				J = R3p_PT_J[i];

			v += N*Math.pow(pi-0.972, 0.5*I)*Math.pow(sig-0.997, J);
		}

		return 0.0041*v;
	}

	function r3q_PT_v(P, T)
	{
		var pi = P/23,
			sig = T/650,
			v = 0,
			R3q_PT_I = [-12,-12,-10,-10,-10,-10,-8,-6,-5,-5,-4,-4,-3,-2,-2,-2,-2,-1,-1,-1,0,1,1,1],
			R3q_PT_J = [10,12,6,7,8,10,8,6,2,5,3,4,3,0,1,2,4,0,1,2,0,0,1,3],
			R3q_PT_N = [-8.2043384325995E4,4.73271518461586E10,-8.05950021005413E-2,3.2860002543598E1,-3.5661702998249E3,-1.72985781433335E9,3.51769232729192E7,-7.75489259985144E5,7.10346691966018E-5,9.93499883820274E4,-6.4209417190457E-1,-6.12842816820083E3,2.32808472983776E2,-1.42808220416837E-5,-6.43596060678456E-3,-4.28577227475614,2.25689939161918E3,1.0035565172151E-3,3.33491455143516E-1,1.09697576888873,9.61917379376452E-1,-8.38165632204598E-2,2.47795908411492,-3.19114969006533E3];

		for(var i = 0; i < 24; i++)
		{
			var N = R3q_PT_N[i],
				I = R3q_PT_I[i],
				J = R3q_PT_J[i];

			v += N*Math.pow(pi-0.848,I)*Math.pow(sig-0.983,J);
		}

		return 0.0022*Math.pow(v,4);
	}

	function r3r_PT_v(P, T)
	{
		var pi = P/23,
			sig = T/650,
			v = 0,
			R3r_PT_I = [-8,-8,-3,-3,-3,-3,-3,0,0,0,0,3,3,8,8,8,8,10,10,10,10,10,10,10,10,12,14],
			R3r_PT_J = [6,14,-3,3,4,5,8,-1,0,1,5,-6,-2,-12,-10,-8,-5,-12,-10,-8,-6,-5,-4,-3,-2,-12,-12],
			R3r_PT_N = [1.44165955660863E-3,-7.01438599628258E12,-8.30946716459219E-17,2.61975135368109E-1,3.93097214706245E2,-1.04334030654021E4,4.90112654154211E8,-1.47104222772069E-4,1.03602748043408,3.05308890065089,-3.99745276971264E6,5.6923371959375E-12,-4.64923504407778E-2,-5.35400396512906E-18,3.99988795693162E-13,-5.36479560201811E-07,1.59536722411202E-2,2.70303248860217E-15,2.44247453858506E-8,-9.83430636716454E-6,6.63513144224454E-2,-9.93456957845006,5.46491323528491E2,-1.43365406393758E4,1.50764974125511E5,-3.37209709340105E-10,3.77501980025469E-9];

		for(var i = 0; i < 27; i++)
		{
			var N = R3r_PT_N[i],
				I = R3r_PT_I[i],
				J = R3r_PT_J[i];

			v += N*Math.pow(pi-0.874,I)*Math.pow(sig-0.982,J);
		}

		return 0.0054*v;
	}

	function r3s_PT_v(P, T)
	{
		var pi = P/21,
			sig = T/640,
			v = 0,
			R3s_PT_I = [-12,-12,-10,-8,-6,-5,-5,-4,-4,-3,-3,-2,-1,-1,-1,0,0,0,0,1,1,3,3,3,4,4,4,5,14],
			R3s_PT_J = [20,24,22,14,36,8,16,6,32,3,8,4,1,2,3,0,1,4,28,0,32,0,1,2,3,18,24,4,24],
			R3s_PT_N = [-5.32466612140254E22,1.00415480000824E31,-1.91540001821367E29,1.05618377808847E16,2.02281884477061E58,8.84585472596134E7,1.66540181638363E22,-3.13563197669111E5,-1.85662327545324E53,-6.24942093918942E-2,-5.0416072413259E9,1.87514491833092E4,1.21399979993217E-3,1.88317043049455,-1.6707350396206E3,9.65961650599775E-1,2.94885696802488,-6.53915627346115E4,6.04012200163444E49,-1.98339358557937E-1,-1.75984090163501E57,3.56314881403987,-5.75991255144384E2,4.56213415338071E4,-1.09174044987829E7,4.37796099975134E33,-6.16552611135792E45,1.93568768917797E9,9.50898170425042E53];

		for(var  i = 0; i < 29; i++)
		{
			var N = R3s_PT_N[i],
				I = R3s_PT_I[i],
				J = R3s_PT_J[i];

			v += N*Math.pow(pi-0.886,I)*Math.pow(sig-0.990,J);
		}

		return 0.0022*Math.pow(v,4);
	}

	function r3t_PT_v(P, T)
	{
		var pi = P/20,
			sig = T/650,
			v = 0,
			R3t_PT_I = [0,0,0,0,1,1,2,2,2,3,3,4,4,7,7,7,7,7,10,10,10,10,10,18,20,22,22,24,28,32,32,32,36],
			R3t_PT_J = [0,1,4,12,0,10,0,6,14,3,8,0,10,3,4,7,20,36,10,12,14,16,22,18,32,22,36,24,28,22,32,36,36],
			R3t_PT_N = [1.55287249586268,6.64235115009031,-2.8936623672721E3,-3.85923202309848E12,-2.91002915783761,-8.29088246858083E11,1.76814899675218,-5.34686695713469E8,1.60464608687834E17,1.96435366560186E5,1.56637427541729E12,-1.78154560260006,-2.29746237623692E15,3.85659001648006E7,1.10554446790543E9,-6.77073830687349E13,-3.27910592086523E30,-3.41552040860644E50,-5.27251339709047E20,2.45375640937055E23,-1.68776617209269E26,3.58958955867578E28,-6.56475280339411E35,3.55286045512301E38,5.6902145441327E57,-7.00584546433113E47,-7.05772623326374E64,1.66861176200148E52,-3.00475129680486E60,-6.68481295196808E50,4.28432338620678E68,-4.44227367758304E71,-2.81396013562745E76];

		for(var i = 0; i < 33; i++)
		{
			var N = R3t_PT_N[i],
				I = R3t_PT_I[i],
				J = R3t_PT_J[i];

			v += N*Math.pow(pi-0.803,I)*Math.pow(sig-1.02,J);
		}

		return 0.0088*v;
	}

	// Auxillary Equations for the region very close to the critical point

	// T3uv ( p) approximates the isochore v = 0.00264 m3 kg-1 from 2.193161551x10^1 MPa to 22.5 MPa.
	function b3uv_P_T(P)
	{
		var B3uv_PT_I = [0,1,2,3],
			B3uv_PT_N = [5.28199646263062E2,8.90579602135307,-0.222814134903755,0.286791682263697E-2],
			T = 0;

		for (var i = 0; i < 4; i++)
		{
			T += B3uv_PT_N[i]*Math.pow(P, B3uv_PT_I[i]);
		}

		return T;
	}

	//T3wx ( p) approximates the isochore v = 0.00385 m3 kg-1 from 2.190096265x10 MPa to 22.5 MPa
	function b3wx_P_T(P)
	{
		var B3wx_PT_I = [0,1,2,-1,-2],
			B3wx_PT_N = [7.2805260914538,9.73505869861952E1,1.47370491183191E1,0.329196213998375E3,8.73371668682417E2],
			T = 0;

		P = Math.log(P);

		for (var i = 0; i < 5; i++)
		{
			T += B3wx_PT_N[i]*Math.pow(P, B3wx_PT_I[i]);
		}

		return T;
	}

	function r3u_PT_v(P, T)
	{
		var pi = P/23,
			sig = T/650,
			v = 0,
			R3u_PT_I = [-12,-10,-10,-10,-8,-8,-8,-6,-6,-5,-5,-5,-3,-1,-1,-1,-1,0,0,1,2,2,3,5,5,5,6,6,8,8,10,12,12,12,14,14,14,14],
			R3u_PT_J = [14,10,12,14,10,12,14,8,12,4,8,12,2,-1,1,12,14,-3,1,-2,5,10,-5,-4,2,3,-5,2,-8,8,-4,-12,-4,4,-12,-10,-6,6],
			R3u_PT_N = [1.22088349258355E17,1.04216468608488E9,-8.82666931564652E15,2.59929510849499E19,2.22612779142211E14,-8.78473585050085E17,-3.14432577551552E21,-2.16934916996285E12,1.59079648196849E20,-3.39567617303423E2,8.84387651337836E12,-8.43405926846418E20,1.14178193518022E1,-1.22708229235641E-4,-1.06201671767107E2,9.03443213959313E24,-6.93996270370852E27,6.48916718965575E-9,7.18957567127851E3,1.05581745346187E-3,-6.51903203602581E14,-1.60116813274676E24,-5.10254294237837E-9,-1.52355388953402E-1,6.77143292290144E11,2.7637843837893E14,1.16862983141686E-2,-3.01426947980171E13,1.6971981388484E-8,1.04674840020929E26,-1.0801690456014E4,-9.90623601934295E-13,5.36116483602738E6,2.26145963747881E21,-4.8873156577621E-10,1.5100154888067E-05,-2.2770046464392E04,-7.81754507698846E27];

		for (var i = 0; i < 38; i++)
		{
			var N = R3u_PT_N[i],
				I = R3u_PT_I[i],
				J = R3u_PT_J[i];

			v += N*Math.pow(pi-0.902,I)*Math.pow(sig-0.988,J);
		}

		return 0.0026*v;
	}

	function r3v_PT_v(P, T)
	{
		var pi = P/23,
			sig = T/650,
			v = 0,
			R3v_PT_I = [-10,-8,-6,-6,-6,-6,-6,-6,-5,-5,-5,-5,-5,-5,-4,-4,-4,-4,-3,-3,-3,-2,-2,-1,-1,0,0,0,1,1,3,4,4,4,5,8,10,12,14],
			R3v_PT_J = [-8,-12,-12,-3,5,6,8,10,1,2,6,8,10,14,-12,-10,-6,10,-3,10,12,2,4,-2,0,-2,6,10,-12,-10,3,-6,3,10,2,-12,-2,-3,1],
			R3v_PT_N = [-4.15652812061591E-55,1.77441742924043E-61,-3.57078668203377E-55,3.59252213604114E-26,-2.59123736380269E1,5.9461976619346E4,-6.24184007103158E10,3.13080299915944E16,1.05006446192036E-9,-1.92824336984852E-6,6.54144373749937E5,5.13117462865044E12,-6.97595750347391E18,-1.03977184454767E28,1.19563135540666E-48,-4.36677034051655E-42,9.26990036530639E-30,5.87793105620748E20,2.80375725094731E-18,-1.92359972440634E22,7.42705723302738E26,-5.17429682450605E1,8.20612048645469E6,-1.88214882341448E-9,1.84587261114837E-2,-1.35830407782663E-6,-7.23681885626348E16,-2.23449194054124E26,-1.11526741826431E-35,2.76032601145151E-29,1.34856491567853E14,6.5244029334586E-10,5.1065511977436E16,-4.68138358908732E31,-7.60667491183279E15,-4.17247986986821E-19,3.12545677756104E13,-1.00375333864186E14,2.47761392329058E26];

		for (var i = 0; i < 39; i++)
		{
			var N = R3v_PT_N[i],
				I = R3v_PT_I[i],
				J = R3v_PT_J[i];

			v += N*Math.pow(pi-0.960, I)*Math.pow(sig-0.995,J);
		}

		return 0.0031*v;
	}

	function r3w_PT_v(P, T)
	{
		var pi = P/23,
			sig = T/650,
			v = 0,
			R3w_PT_I = [-12,-12,-10,-10,-8,-8,-8,-6,-6,-6,-6,-5,-4,-4,-3,-3,-2,-2,-1,-1,-1,0,0,1,2,2,3,3,5,5,5,8,8,10,10],
			R3w_PT_J = [8,14,-1,8,6,8,14,-4,-3,2,8,-10,-1,3,-10,3,1,2,-8,-4,1,-12,1,-1,-1,2,-12,-5,-10,-8,-6,-12,-10,-12,-8],
			R3w_PT_N = [-5.86219133817016E-8,-8.94460355005526E10,5.31168037519774E-31,1.09892402329239E-1,-5.75368389425212E-2,2.28276853990249E4,-1.58548609655002E18,3.29865748576503E-28,-6.34987981190669E-25,6.15762068640611E-9,-9.61109240985747E7,-4.06274286652625E-45,-4.71103725498077E-13,7.25937724828145E-1,1.87768525763682E-39,-1.03308436323771E3,-6.62552816342168E-2,5.7951404176571E2,2.37416732616644E-27,2.71700235739893E-15,-9.0788621348360E1,-1.71242509570207E-37,1.56792067854621E2,9.2326135790147E-1,-5.97865988422577,3.21988767636389E6,-3.99441390042203E-30,4.93429086046981E-08,8.12036983370565E-20,-2.07610284654137E-12,-3.40821291419719E-07,5.42000573372233E-18,-8.56711586510214E-13,2.66170454405981E-14,8.58133791857099E-6];

		for (var i = 0; i < 35; i++)
		{
			var N = R3w_PT_N[i],
				I = R3w_PT_I[i],
				J = R3w_PT_J[i];

			v += N*Math.pow(pi-0.959, I)*Math.pow(sig-0.995, J);
		}

		return 0.0039*Math.pow(v, 4);
	}

	function r3x_PT_v(P, T)
	{
		var pi = P/23,
			sig = T/650,
			v = 0,
			R3x_PT_I = [-8,-6,-5,-4,-4,-4,-3,-3,-1,0,0,0,1,1,2,3,3,3,4,5,5,5,6,8,8,8,8,10,12,12,12,12,14,14,14,14],
			R3x_PT_J = [14,10,10,1,2,14,-2,12,5,0,4,10,-10,-1,6,-12,0,8,3,-6,-2,1,1,-6,-3,1,8,-8,-10,-8,-5,-4,-12,-10,-8,-6],
			R3x_PT_N = [3.77373741298151E18,-5.07100883722913E12,-1.0336322559886E15,1.84790814320773E-6,-9.24729378390945E-4,-4.25999562292738E23,-4.62307771873973E-13,1.07319065855767E21,6.48662492280682E10,2.44200600688281,-8.51535733484258E9,1.69894481433592E21,2.1578022250902E-27,-3.20850551367334E-1,-3.8264244845861E16,-2.75386077674421E-29,-5.63199253391666E5,-3.26068646279314E20,3.97949001553184E13,1.00824008584757E-7,1.62234569738433E4,-4.32355225319745E10,-5.9287424559861E11,1.33061647281106,1.57338197797544E6,2.58189614270853E13,2.62413209706358E24,-9.20011937431142E-2,2.20213765905426E-3,-1.10433759109547E1,8.47004870612087E6,-5.92910695762536E8,-1.8302717326966E-5,1.81339603516302E-1,-1.19228759669889E3,4.30867658061468E6];

		for (var i = 0; i < 36; i++)
		{
			var N = R3x_PT_N[i],
				I = R3x_PT_I[i],
				J = R3x_PT_J[i];

			v += N*Math.pow(pi-0.910, I)*Math.pow(sig-0.988, J);
		}

		return 0.0049*v;
	}

	function r3y_PT_v(P, T)
	{
		var pi = P/22,
			sig = T/650,
			v = 0,
			R3y_PT_I = [0,0,0,0,1,2,2,2,2,3,3,3,4,4,5,5,8,8,10,12],
			R3y_PT_J = [-3,1,5,8,8,-4,-1,4,5,-8,4,8,-6,6,-2,1,-8,-2,-5,-8],
			R3y_PT_N = [-5.25597995024633E-10,5.83441305228407E3,-1.34778968457925E16,1.18973500934212E25,-1.59096490904708E26,-3.15839902302021E-7,4.96212197158239E2,3.27777227273171E18,-5.27114657850696E21,2.10017506281863E-17,7.05106224399834E20,-2.66713136106469E30,-1.45370512554562E-8,1.4933391705313E27,-1.49795620287641E7,-3.818819062711E15,7.24660165585797E-5,-9.37808169550193E13,5.14411468376383E9,-8.28198594040141E4];

		for (var i = 0; i < 20; i++)
		{
			var N = R3y_PT_N[i],
				I = R3y_PT_I[i],
				J = R3y_PT_J[i];

			v += N*Math.pow(pi-0.996, I)*Math.pow(sig-0.994, J);
		}

		return 0.0031*Math.pow(v,4);
	}

	function r3z_PT_v(P, T)
	{
		var pi = P/22,
			sig = T/650,
			v = 0,
			R3z_PT_I = [-8,-6,-5,-5,-4,-4,-4,-3,-3,-3,-2,-1,0,1,2,3,3,6,6,6,6,8,8],
			R3z_PT_J = [3,6,6,8,5,6,8,-2,5,6,2,-6,3,1,6,-6,-2,-6,-5,-4,-1,-8,-4],
			R3z_PT_N = [2.4400789229065E-11,-4.63057430331242E6,7.28803274777712E9,3.27776302858856E15,-1.10598170118409E9,-3.23899915729957E12,9.23814007023245E15,8.42250080413712E-13,6.63221436245506E11,-1.67170186672139E14,2.53749358701391E3,-8.19731559610523E-21,3.28380587890663E11,-6.25004791171543E7,8.03197957462023E20,-2.04397011338353E-11,-3.78391047055938E3,9.7287654593862E-3,1.54355721681459E1,-3.73962862928643E3,-6.82859011374572E10,-2.48488015614543E-4,3.94536049497068E6];

		for(var i = 0; i < 23; i++)
		{
			var N = R3z_PT_N[i],
				I = R3z_PT_I[i],
				J = R3z_PT_J[i];

			v += N*Math.pow(pi-0.993, I)*Math.pow(sig-0.994, J);
		}

		return 0.0038*Math.pow(v,4);
	}

	//
	// Region 4
	//

	// The saturation pressure for given temperature along the vapour-liquid saturation line
	// Range of validity is 273.15 K <= T <= 647.096 K
	// Equation 30 (pp. 33)
	function r4_T_Psat(T)
	{
		var Tr = T,
			v = Tr + -0.238558557567849/(Tr-650.17534844798),
			A = v*v + 1167.0521452767*v + -724213.16703206,
			B = -17.073846940092*v*v + 12020.82470247*v + -3232555.0322333,
			C = 14.91510861353*v*v + -4823.2657361591*v + 405113.40542057;

		return Math.pow(2*C/(-B+Math.pow(B*B-4*A*C,0.5)),4);
	}

	// The saturation temperature for given pressure along the vapour-liquid saturation line
	// Range of validity is 0.000611213 MPa (611.213 Pa) <= p <= 22.064 MPa
	// Equation 30 (pp. 33)
	function r4_P_Tsat(P)
	{

		var R4_PT_N = [1167.0521452767,-724213.16703206,-17.073846940092,12020.82470247,-3232555.0322333,14.91510861353,-4823.2657361591,405113.40542057,-0.238558557567849,650.17534844798],
			b = Math.pow(P,0.25),
			E = b*b + R4_PT_N[2]*b +R4_PT_N[5],
			F = R4_PT_N[0]*b*b + R4_PT_N[3]*b +R4_PT_N[6],
			G = R4_PT_N[1]*b*b + R4_PT_N[4]*b + R4_PT_N[7],
			D = 2.0*G/(-F-Math.pow(F*F-4.0*E*G,0.5));

		return (R4_PT_N[9] + D -Math.pow(Math.pow(R4_PT_N[9]+D,2)-4.0*(R4_PT_N[8]+R4_PT_N[9]*D),0.5))/2.0;
	}

	//
	// Region 5
	//
	function r5_PT(P, T)
	{
		var R5I_PT_N = [-13.179983674201, 6.8540841634434, -0.024805148933466, 0.36901534980333, -3.1161318213925, -0.32961626538917],
			R5I_PT_J = [0, 1, -3, -2, -1, 2],
			R5R_PT_N = [1.5736404855259E-3, 9.0153761673944E-4, -5.0270077677648E-3, 2.2440037409485E-6, -4.1163275453471E-6, 3.7919454822955E-8],
			R5R_PT_J = [1, 2, 3, 3, 9, 7],
			R5R_PT_I = [1, 1, 1, 2, 2, 3],
			pi = P,
			tau = 1000/T,
			gi = Math.log(pi),
			gi_p = 1/pi,
			gi_pp = -1/(pi*pi),
			gi_t = 0,
			gi_tt = 0,
			gi_pt = 0,
			N, J, I;

		for (var i = 0; i < 6; i++)
		{
			N = R5I_PT_N[i];
			J = R5I_PT_J[i];

			gi += N*Math.pow(tau,J);
			gi_t += N*J*Math.pow(tau,J-1);
			gi_tt += N*J*(J-1)*Math.pow(tau,J-2);
		}

		var gr = 0,
			gr_p = 0,
			gr_pp = 0,
			gr_t = 0,
			gr_tt = 0,
			gr_pt = 0;

		for (var j = 0; j < 6; j++)
		{
			N = R5R_PT_N[j];
			J = R5R_PT_J[j];
			I = R5R_PT_I[j];

			gr += N*Math.pow(pi,I)*Math.pow(tau,J);
			gr_p += N*I*Math.pow(pi,I-1)*Math.pow(tau,J);
			gr_pp += N*I*(I-1)*Math.pow(pi,I-2)*Math.pow(tau,J);
			gr_t += N*Math.pow(pi,I)*J*Math.pow(tau,J-1);
			gr_tt += N*Math.pow(pi,I)*J*(J-1)*Math.pow(tau,J-2);
			gr_pt += N*I*Math.pow(pi,I-1)*J*Math.pow(tau,J-1);
		}

		// The pressure and temperature MPa, K
		return generateResult({
			p: P,
			t: T,
			v: R*T*pi*(gi_p+gr_p)/(1000*P),
			u: R*T*(tau*(gi_t+gr_t)-pi*(gi_p+gr_p)),
			s: R*(tau*(gi_t+gr_t)-(gi+gr)),
			h: R*T*tau*(gi_t+gr_t),
			cp: -R*tau*tau*(gi_tt+gr_tt),
			cv: R*(-tau*tau*(gi_tt+gr_tt)-Math.pow(1+pi*gr_p-tau*pi*gr_pt,2)/(1-pi*pi*gr_pp)),
			w: Math.pow(1000*R*T*((1+2*pi*gr_p+pi*pi*gr_p*gr_p) / ((1-pi*pi*gr_pp) + Math.pow(1+pi*gr_p-tau*pi*gr_pt,2)/(tau*tau*(gi_tt+gr_tt)))),0.5),
		});
	}
}));




